package priv.yiyuan.example.design.chain;

/**
 * 折扣处理器
 *
 * @author YiYuan
 * @data 2025/10/11
 * @apoNote
 */
public abstract class DiscountHandler {

    protected DiscountHandler nextHandler;

    protected DiscountStrategy strategy;

    public DiscountHandler(DiscountStrategy strategy) {
        this.strategy = strategy;
    }

    public void setNextHandler(DiscountHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    public void handleDiscount(Order order) {
        double discount = strategy.calculateDiscount(order);
        if (discount > 0) {
            order.setFinalPrice(order.getFinalPrice() - discount);
            System.out.println("当前订单最终价格变为: " + String.format("%.2f", order.getFinalPrice()) + "元。");
        }

        if (nextHandler != null) {
            nextHandler.handleDiscount(order);
        }
    }

}
